package br.com.medmark.model.hibernate.dao;

import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;

import br.com.medmark.model.hibernate.Clinica;
import br.com.medmark.model.hibernate.Profissional;

public class ClinicaDAO extends GenericoDAOImpl<Clinica>{
	
	private static final long serialVersionUID = 7125207808799640026L;
	private static ClinicaDAO cliDAO = null;
	
	private ClinicaDAO(){
		
	}
	
	public static ClinicaDAO getInstance(){
		
		if ( cliDAO == null) {
			cliDAO = new ClinicaDAO();
		}
	
		return cliDAO;
		
	}
	public Clinica getClinica(String login, String senha) {
		String sql = "FROM Clinica c Where c.login = :login and c.senha = :senha";
		
		TypedQuery<Clinica> query = this.entityManager.createQuery(sql, Clinica.class);
		query.setParameter("login", login);
		query.setParameter("senha", senha);
		
		try {
			return query.getSingleResult();
		} catch (NoResultException ex) {
			return null;
		}
	}
	
	public Clinica getClinica(int codigo) {
		String sql = "FROM Clinica c Where c.codigo = :codigo";
		
		TypedQuery<Clinica> query = this.entityManager.createQuery(sql, Clinica.class);
		query.setParameter("codigo", codigo);
		
		try {
			return query.getSingleResult();
		} catch (NoResultException ex) {
			return null;
		}
	}
	
	public List<Profissional> listarProfissionais(int codigo){
		String sql = "SELECT Profissional p "
				+ "FROM Alocacao a, Profissional p, Clinica c "
				+ "WHERE a.clinica.codigo = :codigo "
				+ "AND a.profissional.codigo = p.codigo";
		
		TypedQuery<Profissional> query = this.entityManager.createQuery(sql, Profissional.class);
		query.setParameter("codigo", codigo);
		
		return query.getResultList();
		
	}
	
}
